package com.loren.demo13.controller;

import com.loren.demo13.data.entity.Article;
import com.loren.demo13.data.form.ArticleForm;
import com.loren.demo13.service.ArticleService;
import com.loren.demo13.utils.MPage;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@AllArgsConstructor
@RequestMapping("/article")
public class ArticleController {

    private final ArticleService articleService;

    @GetMapping("/list")
    public MPage<Article> articles(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                   @RequestParam(value = "size", required = false, defaultValue = "5") Integer size) {
        return this.articleService.getArticleList(page, size);
    }

    @GetMapping("/all")
    public List<Map<String, Object>> articles() {
        return this.articleService.getArticleList();
    }

    @PostMapping
    public String saveArticleInfo(@RequestBody @Validated ArticleForm form) {
        this.articleService.saveArticle(form);
        return "success";
    }

    @PutMapping("/{id}")
    public String updateArticleInfo(@PathVariable("id") Long articleId, @RequestBody @Validated ArticleForm form) {
        this.articleService.updateArticle(articleId, form);
        return "success";
    }

}
